package com.ymatou.iapi.couponactivity.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponactivity.parameter.NewUserMobileBoundBean;
import com.ymatou.iapi.couponactivity.service.NewUserMobileBoundCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.YmtCouponActivity;
import com.ymttest.database.model.YmtCouponActivityNewUserBound;
import com.ymttest.database.sqlwapper.YmtCouponActivityWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*新用户手机绑定用例
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_NewUserMobileBound{
	private static NewUserMobileBoundBean newusermobileboundBean;
	private static NewUserMobileBoundCall newusermobileboundCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("新用户手机绑定用例");
	}

	@Before
	public void caseUp() {
		newusermobileboundBean=new NewUserMobileBoundBean();
		newusermobileboundCall=new NewUserMobileBoundCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(newusermobileboundCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_NewUserMobileBound_001() {
		Logger.start(true,"新用户手机绑定成功");
		try {
			
			YmtCouponActivity activity = new YmtCouponActivityWapper().selectCouponActivityByActivityChannel("common").get(0);
			
			String mobilePhone = TestDataManager.getRandomNum(11);
			
			newusermobileboundBean.setMobilePhone(mobilePhone);
			newusermobileboundBean.setActivityChannel(activity.getActivityChannel());
			newusermobileboundCall.setData(newusermobileboundBean);
			newusermobileboundCall.callService();	
			
			Logger.verifyEquals("0", newusermobileboundCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", newusermobileboundCall.getMessage(), "验证返回Message");

			List<YmtCouponActivityNewUserBound> bounds = new YmtCouponActivityWapper().selectCouponActivityNewUserBoundByMobilePhoneAndActivityCode(mobilePhone, activity.getActivityCode());
			
			Logger.verifyEquals(1, bounds.size(), "验证数据库表Ymt_CouponActivityNewUserBound生成一条绑定记录");
			
			if(bounds.size()==1)
			{
				YmtCouponActivityNewUserBound bound = bounds.get(0);
				Logger.verifyEquals(1, bound.getCouponSendStatus().intValue(), "验证数据库表Ymt_CouponActivityNewUserBound中字段CouponSendStatus为1");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	@Ignore
	public void Tc_NewUserMobileBound_002() {
		Logger.start(false,"ChangeMobileEventSource=null时不发券");
		try {
			
			newusermobileboundBean.setMobilePhone(EnvSetup.getData("BuyUserPhone"));
			newusermobileboundBean.setActivityChannel("");
			
			newusermobileboundCall.setData(newusermobileboundBean);
			newusermobileboundCall.callService();			
			
			Logger.verifyEquals("11002", newusermobileboundCall.getCode(), "验证返回code");
			Logger.verifyEquals("手机号码已经注册", newusermobileboundCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_NewUserMobileBound_003() {
		Logger.start(false,"手机号码未填");
		try {
			
			newusermobileboundBean.setActivityChannel("1");
			
			newusermobileboundCall.setData(newusermobileboundBean);
			newusermobileboundCall.callService();			
			
			Logger.verifyEquals("2", newusermobileboundCall.getCode(), "验证返回code");
			Logger.verifyEquals("手机号码不能为空", newusermobileboundCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_NewUserMobileBound_004() {
		Logger.start(false,"活动渠道未填-默认Common");
		try {

			YmtCouponActivity activity = new YmtCouponActivityWapper().selectCouponActivityByActivityChannel("Common").get(0);
			
			String mobilePhone = TestDataManager.getRandomNum(11);
			
			newusermobileboundBean.setMobilePhone(mobilePhone);
			
			newusermobileboundCall.setData(newusermobileboundBean);
			newusermobileboundCall.callService();	
			
			Logger.verifyEquals("0", newusermobileboundCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", newusermobileboundCall.getMessage(), "验证返回Message");

			List<YmtCouponActivityNewUserBound> bounds = new YmtCouponActivityWapper().selectCouponActivityNewUserBoundByMobilePhoneAndActivityCode(mobilePhone, activity.getActivityCode());
			
			Logger.verifyEquals(1, bounds.size(), "验证数据库表Ymt_CouponActivityNewUserBound生成一条绑定记录");
			
			if(bounds.size()==1)
			{
				YmtCouponActivityNewUserBound bound = bounds.get(0);
				Logger.verifyEquals(1, bound.getCouponSendStatus().intValue(), "验证数据库表Ymt_CouponActivityNewUserBound中字段CouponSendStatus为1");
			}	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_NewUserMobileBound_005() {
		Logger.start(false,"活动渠道不存在");
		try {
			newusermobileboundBean.setMobilePhone("15611111111");
			newusermobileboundBean.setActivityChannel("1");
			
			newusermobileboundCall.setData(newusermobileboundBean);
			newusermobileboundCall.callService();			
			
			Logger.verifyEquals("10001", newusermobileboundCall.getCode(), "验证返回code");
			Logger.verifyEquals("未找到有效的活动信息", newusermobileboundCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_NewUserMobileBound_006() {
		Logger.start(false,"活动渠道传空字符串-默认Common");
		try {

			YmtCouponActivity activity = new YmtCouponActivityWapper().selectCouponActivityByActivityChannel("Common").get(0);
			
			String mobilePhone = TestDataManager.getRandomNum(11);
			
			newusermobileboundBean.setMobilePhone(mobilePhone);
			newusermobileboundBean.setActivityChannel("");
			newusermobileboundCall.setData(newusermobileboundBean);
			newusermobileboundCall.callService();	
			
			Logger.verifyEquals("0", newusermobileboundCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", newusermobileboundCall.getMessage(), "验证返回Message");

			List<YmtCouponActivityNewUserBound> bounds = new YmtCouponActivityWapper().selectCouponActivityNewUserBoundByMobilePhoneAndActivityCode(mobilePhone, activity.getActivityCode());
			
			Logger.verifyEquals(1, bounds.size(), "验证数据库表Ymt_CouponActivityNewUserBound生成一条绑定记录");
			
			if(bounds.size()==1)
			{
				YmtCouponActivityNewUserBound bound = bounds.get(0);
				Logger.verifyEquals(1, bound.getCouponSendStatus().intValue(), "验证数据库表Ymt_CouponActivityNewUserBound中字段CouponSendStatus为1");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_NewUserMobileBound_007() {
		Logger.start(false,"手机号码已经绑定过");
		try {
			YmtCouponActivityNewUserBound bound = new YmtCouponActivityWapper().selectTop1CouponActivityNewUserBound().get(0);
			
			newusermobileboundBean.setMobilePhone(bound.getMobilePhone());
			newusermobileboundBean.setActivityChannel("");
			
			newusermobileboundCall.setData(newusermobileboundBean);
			newusermobileboundCall.callService();			
			
			Logger.verifyEquals("11001", newusermobileboundCall.getCode(), "验证返回code");
			Logger.verifyEquals("手机号码已经绑定过", newusermobileboundCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_NewUserMobileBound_008() {
		Logger.start(false,"反例-用户存在风险-NewUserMobileBound不检查风控");
		try {
			
			YmtCouponActivity activity = new YmtCouponActivityWapper().selectCouponActivityByActivityChannel("common").get(0);
			
			newusermobileboundBean.setMobilePhone("18516029204");
			newusermobileboundBean.setActivityChannel(activity.getActivityChannel());
			newusermobileboundCall.setData(newusermobileboundBean);
			newusermobileboundCall.callService();	
			
			Logger.verifyEquals("0", newusermobileboundCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", newusermobileboundCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}